home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
security
/
log_tcp_6.0alpha.shar
/
fromhost.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-02
|
4KB
|
144 lines
/*
* fromhost() determines the type of connection (datagram, stream), the name
* and address of the host at the other end of standard input, and the
* remote user name (if RFC 931 lookups are enabled). A host name of "stdin"
* is returned if the program is run from a tty. The value "unknown" is
* returned as a placeholder for information that could not be looked up (an
* empty username means that lookup was not even attempted). All results are
* in static or allocated memory.
*
* On systems that have TLI as the official network programming interface, the
* software attempts to determine the underlying transport protocol family.
* If that happens to be IP, it immedately falls back to familiar concepts
* and primitives: sockets, DNS, and so on. In all other cases the network
* address results will be in some sort of transport-independent form, and
* functionality will be limited (no remote username lookups, no net/mask
* address patterns).
*
* The return status is (-1) if the remote host pretends to have someone elses
* host name, otherwise a zero status is returned.
*
* Diagnostics are reported through syslog(3).
*
* Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
*/
#ifndef lint
static char sccsid[] = "@(#) fromhost.c 1.8 93/07/02 16:33:25";
#endif
/* System libraries. */
#include <sys/types.h>
#include <stdio.h>
#include <syslog.h>
#ifdef TLI
#include <sys/tiuser.h>
#include <stropts.h>
#endif
/* Local stuff. */
#include "log_tcp.h"
/* Initially, we know nothing about the origin of the connection. */
static struct from_host from_unknown = {
0, /* connected/unconnected */
FROM_UNKNOWN, /* remote host name */
FROM_UNKNOWN, /* remote host address */
"", /* remote user name */
};
/* fromhost - find out what is at the other end of standard input */
int fromhost(f)
struct from_host *f;
{
/*
* Initialize all results to UNKNOWN.
*/
*f = from_unknown;
#ifndef TLI
/*
* Assume that stdin is always a TCP/IP socket. If you get errors like
* "getpeername: No such file or directory", you may have to enable TLI
* support anyway.
*/
return (from_sock(f));
#else
/*
* Systems with streams support can have different programmatic
* interfaces to the same network protocol. On systems with streams
* support, standard input will not necessarily always be a stream.
*
* Thus, we must first find out what programmatic interface to use: sockets
* or (transport-independent) TLI. On some systems, sockets are not part
* of the streams system, so if stdin is not a stream we assume sockets.
*
* If the transport protocol underneath TLI is not IP, we will end up with
* some magic cookies for addresses, but that is still better than
* knowing nothing.
*/
if (!isastream(0))
return (from_sock(f));
if (ioctl(0, I_FIND, "timod") > 0)
return (from_tli(f));
if (ioctl(0, I_FIND, "sockmod") > 0)
return (from_sock(f));
syslog(LOG_ERR, "fromhost: cannot establish type of transport interface");
return (0);
#endif
}
#ifdef TEST
/* Code for stand-alone testing. */
main(argc, argv)
int argc;
char **argv;
{
struct from_host from;
#ifdef LOG_MAIL
(void) openlog(argv[0], LOG_PID, FACILITY);
#else
(void) openlog(argv[0], LOG_PID);
#endif
/*
* Turn on the "IP-underneath-TLI" detection heuristics.
*/
#ifdef TLI
if (ioctl(0, I_FIND, "timod") == 0)
ioctl(0, I_PUSH, "timod");
#endif /* TLI */
/*
* Force remote username lookups.
*/
(void) fromhost(&from);
if (from.user[0] == 0 && from.sock_type == FROM_CONNECTED && from.sin)
from.user = rfc931_name(from.sin);
/*
* Show some results.
*/
printf("address: %s\n", from.addr);
printf("hostname: %s\n", from.name);
printf("username: %s\n", from.user);
printf("hostsinfo: %s\n", hosts_info(&from));
return (0);
}
#endif /* TEST */